package com.mdata.model.vo;

import com.mdata.model.dto.wechat.result.Department;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class DepartmentTreeVO {

    private Integer id;

    private String name;

    private Integer parentId;

    private List<DepartmentTreeVO> childrens;

    @Override
    public String toString() {
        return "DepartmentTreeVO{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", parentId=" + parentId +
                ", childrens=" + childrens +
                '}';
    }

    public static DepartmentTreeVO fromDepartment(Department department){
        DepartmentTreeVO departmentTreeVO = new DepartmentTreeVO();
        BeanUtils.copyProperties(department, departmentTreeVO);

        departmentTreeVO.setChildrens(fromDepartmentList(department.getChildrens()));
        return departmentTreeVO;
    }

    public static List<DepartmentTreeVO> fromDepartmentList(List<Department> departmentList){
        if(CollectionUtils.isEmpty(departmentList)){
            return null;
        }
        return departmentList.stream().map(o -> fromDepartment(o)).collect(Collectors.toList());
    }
}
